package com.app.service.Impl;

import cn.hutool.core.date.DateUtil;
import com.app.common.Result;
import com.app.mapper.*;
import com.app.model.*;
import com.app.service.StatisticsService;
import com.app.vo.MapVo;
import com.app.vo.OverviewVo;
import com.app.vo.StatisticsVo;
import com.app.vo.ValueAndNameVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityEnlistMapper activityEnlistMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StudyMapper studyMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsExchangeMapper goodsExchangeMapper;

    @Autowired
    private SystemMapper systemMapper;

    @Override
    public StatisticsVo selectCount(Integer villageId) {
        LocalDate now = LocalDate.now();
        YearMonth yearMonth = YearMonth.from(now);
        StatisticsVo vo = new StatisticsVo();
        //活动
        QueryWrapper<ActivityModel> queryWrapper = new QueryWrapper<>();
        if (villageId != null && villageId != 0) {
            queryWrapper.eq("village_id", villageId);
        }
        queryWrapper.like("create_time", yearMonth);
        Integer activityThisMonthCount = activityMapper.selectCount(queryWrapper);
        QueryWrapper<ActivityModel> activity = new QueryWrapper<>();
        Integer activityCount;
        if (villageId != null && villageId != 0) {
            activity.eq("village_id", villageId);
            activityCount = activityMapper.selectCount(activity);
        } else {
            activityCount = activityMapper.selectCount(null);
        }
        //文章
        QueryWrapper<StudyModel> queryWrapper1 = new QueryWrapper<>();
        if (villageId != null && villageId != 0) {
            queryWrapper1.eq("village_id", villageId);
        }
        queryWrapper1.like("create_time", yearMonth);
        Integer studyThisMonthCount = studyMapper.selectCount(queryWrapper1);
        QueryWrapper<StudyModel> study = new QueryWrapper<>();
        Integer studyCount;
        if (villageId != null && villageId != 0) {
            study.eq("village_id", villageId);
            studyCount = studyMapper.selectCount(study);
        } else {
            studyCount = studyMapper.selectCount(null);
        }
        //用户
        QueryWrapper<UserModel> queryWrapper2 = new QueryWrapper<>();
        if (villageId != null && villageId != 0) {
            queryWrapper2.eq("village_id", villageId);
        }
        queryWrapper2.like("create_time", yearMonth);
        Integer userThisMonthCount = userMapper.selectCount(queryWrapper2);
        QueryWrapper<UserModel> user = new QueryWrapper<>();
        Integer userCount;
        if (villageId != null && villageId != 0) {
            user.eq("village_id", villageId);
            userCount = userMapper.selectCount(user);
        } else {
            userCount = userMapper.selectCount(null);
        }
        //商品
        QueryWrapper<GoodsModel> queryWrapper3 = new QueryWrapper<>();
        if (villageId != null && villageId != 0) {
            queryWrapper3.eq("village_id", villageId);
        }
        queryWrapper3.like("create_time", yearMonth);
        Integer goodsThisMonthCount = goodsMapper.selectCount(queryWrapper3);
        QueryWrapper<GoodsModel> goods = new QueryWrapper<>();
        Integer goodsCount;
        if (villageId != null && villageId != 0) {
            goods.eq("village_id", villageId);
            goodsCount = goodsMapper.selectCount(goods);
        } else {
            goodsCount = goodsMapper.selectCount(null);
        }

        vo.setActivityCount(activityCount);
        vo.setActivityThisMonthCount(activityThisMonthCount);
        vo.setStudyCount(studyCount);
        vo.setStudyThisMonthCount(studyThisMonthCount);
        vo.setUserCount(userCount);
        vo.setUserThisMonthCount(userThisMonthCount);
        vo.setGoodsCount(goodsCount);
        vo.setGoodsThisMonthCount(goodsThisMonthCount);
        return vo;
    }

    @Override
    public List<ValueAndNameVo> getActivityTypeCount(Integer villageId) {
        List<SystemModel> typeList = activityMapper.selectTypeList();
        List<ValueAndNameVo> vo = new ArrayList<>();
        for (int i = 0; i < typeList.size(); i++) {
            QueryWrapper<ActivityModel> queryWrapper = new QueryWrapper<>();
            ValueAndNameVo vo1 = new ValueAndNameVo();
            queryWrapper.eq("type", typeList.get(i).getId());
            if (villageId != null && villageId != 0) {
                queryWrapper.eq("village_id", villageId);
            }
            Integer count = activityMapper.selectCount(queryWrapper);
            vo1.setName(typeList.get(i).getName());
            vo1.setValue(count);
            vo.add(vo1);
        }
        return vo;
    }

    @Override
    public List<ValueAndNameVo> getStudyTypeCount(Integer villageId) {
        List<SystemModel> typeList = studyMapper.selectTypeList();
        List<ValueAndNameVo> vo = new ArrayList<>();
        for (int i = 0; i < typeList.size(); i++) {
            QueryWrapper<StudyModel> queryWrapper = new QueryWrapper<>();
            ValueAndNameVo vo1 = new ValueAndNameVo();
            queryWrapper.eq("type", typeList.get(i).getId());
            if (villageId != null && villageId != 0) {
                queryWrapper.eq("village_id", villageId);
            }
            Integer count = studyMapper.selectCount(queryWrapper);
            vo1.setName(typeList.get(i).getName());
            vo1.setValue(count);
            vo.add(vo1);
        }
        return vo;
    }

    @Override
    public List<ValueAndNameVo> getGoodsTypeCount(Integer villageId) {
        List<SystemModel> typeList = goodsMapper.selectTypeList();
        List<ValueAndNameVo> vo = new ArrayList<>();
        for (int i = 0; i < typeList.size(); i++) {
            QueryWrapper<GoodsModel> queryWrapper = new QueryWrapper<>();
            ValueAndNameVo vo1 = new ValueAndNameVo();
            queryWrapper.eq("type", typeList.get(i).getId());
            if (villageId != null && villageId != 0) {
                queryWrapper.eq("village_id", villageId);
            }
            Integer count = goodsMapper.selectCount(queryWrapper);
            vo1.setName(typeList.get(i).getName());
            vo1.setValue(count);
            vo.add(vo1);
        }
        return vo;
    }

    @Override
    public Object activityFifteenDays(Integer villageId) {
        Map<String, int[]> countMap = new HashMap<>();
        Map<String, Integer> countData = new HashMap<>();
        for (int i = 0; i < 15; i++) {
            int[] value = new int[3];
            int allCount = 0;
            String time = DateUtil.formatDate(com.app.util.DateUtil.addDays(new Date(), -i));
            QueryWrapper<ActivityEnlistModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("create_time", time);
//            if (villageId != null && villageId != 0) {
//                queryWrapper.eq("village_id", villageId);
//            }
            List<ActivityEnlistModel> activityEnlistModels = activityEnlistMapper.selectList(queryWrapper);
            for (int i1 = 0; i1 < activityEnlistModels.size(); i1++) {
                if (activityEnlistModels.get(i1).getStatus() == 0) {
                    value[0]++;
                } else if (activityEnlistModels.get(i1).getStatus() == 1) {
                    value[1]++;
                } else if (activityEnlistModels.get(i1).getStatus() == 2) {
                    value[2]++;
                }
            }
            countMap.put(time, value);

            int count = activityEnlistMapper.selectCount(queryWrapper);
            allCount += count;
            countData.put(time, allCount);
        }
        TreeMap<String, Integer> sortedMap = new TreeMap<>(countData);
        TreeMap<String, int[]> sortedMap1 = new TreeMap<>(countMap);
        MapVo vo = new MapVo();
        vo.setTypeData(sortedMap1);
        vo.setNumData(sortedMap);
        return vo;
    }

    @Override
    public Object goodsFifteenDays(Integer villageId) {
        Map<String, int[]> countMap = new HashMap<>();
        Map<String, Integer> countData = new HashMap<>();
        for (int i = 0; i < 15; i++) {
            int[] value = new int[3];
            int allCount = 0;
            String time = DateUtil.formatDate(com.app.util.DateUtil.addDays(new Date(), -i));
            QueryWrapper<GoodsExchangeModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("create_time", time);
//            if (villageId != null && villageId != 0) {
//                queryWrapper.eq("village_id", villageId);
//            }
            List<GoodsExchangeModel> goodsExchangeModels = goodsExchangeMapper.selectList(queryWrapper);
            for (int i1 = 0; i1 < goodsExchangeModels.size(); i1++) {
                if (goodsExchangeModels.get(i1).getStatus() == 0) {
                    value[0]++;
                } else if (goodsExchangeModels.get(i1).getStatus() == 1) {
                    value[1]++;
                } else if (goodsExchangeModels.get(i1).getStatus() == 2) {
                    value[2]++;
                }
            }
            countMap.put(time, value);

            int count = goodsExchangeMapper.selectCount(queryWrapper);
            allCount += count;
            countData.put(time, allCount);
        }
        TreeMap<String, Integer> sortedMap = new TreeMap<>(countData);
        TreeMap<String, int[]> sortedMap1 = new TreeMap<>(countMap);
        MapVo vo = new MapVo();
        vo.setTypeData(sortedMap1);
        vo.setNumData(sortedMap);
        return vo;
    }

    @Override
    public Object getUserOverview(Integer villageId) {
        // 注册用户数量、报名用户数量、获取积分用户、实名用户数量、消费积分用户、文章浏览数量
        QueryWrapper<UserModel> queryWrapper3 = new QueryWrapper<>();
        Integer userCount;
        if (villageId != null && villageId != 0) {
            queryWrapper3.eq("village_id", villageId);
            userCount = userMapper.selectCount(queryWrapper3);//注册用户数量
        } else {
            userCount = userMapper.selectCount(null);//注册用户数量
        }
        QueryWrapper<ActivityEnlistModel> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("status", 1);
//        if (villageId != null && villageId != 0) {
//            queryWrapper2.eq("village_id", villageId);
//        }
        Integer activityEnlistCount = activityEnlistMapper.selectCount(queryWrapper2);//报名用户数量
        Integer studySum = studyMapper.studySum();//文章浏览数量
        QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("id_card");
        queryWrapper.ne("id_card", "");
//        if (villageId != null && villageId != 0) {
//            queryWrapper.eq("village_id", villageId);
//        }
        Integer realUserCount = userMapper.selectCount(queryWrapper);//实名用户数量
        QueryWrapper<UserModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.notIn("integral", 0);
        if (villageId != null && villageId != 0) {
            queryWrapper1.eq("village_id", villageId);
        }
        Integer getIntegralCount = userMapper.selectCount(queryWrapper1);//获取积分用户
        List<GoodsExchangeModel> consumption = goodsExchangeMapper.getConsumption(villageId);
        Set<Integer> set = consumption
                .stream()
                .map(GoodsExchangeModel::getUserId)
                .collect(Collectors.toSet());
        OverviewVo vo = new OverviewVo();
        vo.setRegisterCount(userCount);
        vo.setApplicationCount(activityEnlistCount);
        vo.setArticleViewCount(studySum);
        vo.setRealNameCount(realUserCount);
        vo.setGetIntegralCount(getIntegralCount);
        vo.setConsumerUsersCount(set.size());
        return vo;
    }

    @Override
    public Object getIntegralList(Integer villageId) {
        List<UserModel> integralList;
        if (villageId != null && villageId != 0) {
            integralList = userMapper.getIntegralList(villageId);
        } else {
            integralList = userMapper.getIntegralList();
        }
        List<UserModel> collect = integralList.stream().limit(10).collect(Collectors.toList());
        for (int i = 0; i < collect.size(); i++) {
            collect.get(i).setRanking(i + 1);
        }
        return collect;
    }

    @Override
    public Object getConsumption(Integer villageId) {
        List<GoodsExchangeModel> consumption = goodsExchangeMapper.getConsumption(villageId);
        List<GoodsExchangeModel> list = new ArrayList<>();
        for (int i = 0; i < consumption.size(); i++) {
            Integer goodsId = consumption.get(i).getGoodsId();
            Integer num = consumption.get(i).getNum();
            GoodsModel goodsModel = goodsMapper.selectById(goodsId);
            Integer integral = goodsModel.getIntegral();
            consumption.get(i).setBonusPoints(integral * num);
        }
        Set<Integer> set = consumption
                .stream()
                .map(GoodsExchangeModel::getUserId)
                .collect(Collectors.toSet());
        List<Integer> collect = set.stream().collect(Collectors.toList());
        for (int i = 0; i < collect.size(); i++) {
            GoodsExchangeModel model = new GoodsExchangeModel();
            model.setUserId(collect.get(i));
            list.add(model);
        }

        for (int i = 0; i < list.size(); i++) {
            int sum = 0;
            for (int i1 = 0; i1 < consumption.size(); i1++) {
                if (list.get(i).getUserId().equals(consumption.get(i1).getUserId())) {
                    sum = sum + consumption.get(i1).getBonusPoints();
                    list.get(i).setBonusPoints(sum);
                }
            }
        }
        List<GoodsExchangeModel> collect1 = list.stream().limit(10).collect(Collectors.toList());
        for (int i = 0; i < collect1.size(); i++) {
            Integer userId = collect1.get(i).getUserId();
            UserModel model = userMapper.selectById(userId);
            collect1.get(i).setName(model.getName());
            collect1.get(i).setPhoneNumber(model.getPhoneNumber());
            collect1.get(i).setRanking(i + 1);
        }
        return collect1;
    }
}
